package com.hot12345.springmvc.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hot12345.common.spring.SpringAbstractController;
import com.hot12345.dao.ItemDaoImpl;
import com.hot12345.entity.Item;

@Controller
@RequestMapping("/admin/item")
public class ItemController extends SpringAbstractController {

	@RequestMapping("/index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("state_gte",0);
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		List<Item> list = itemDao.findByParams(params);
		
		modelMap.put("itemList", list);
		return new ModelAndView("admin/item/itemList");
	}
	
	@RequestMapping(value="/add", method = RequestMethod.GET)
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
		return new ModelAndView("admin/item/itemAdd");
	}
	
	@RequestMapping(value="/edit", method = RequestMethod.GET)
	public ModelAndView edit(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		Item item = itemDao.findByPrimaryKey(id);
		modelMap.put("item", item);
		
		return new ModelAndView("admin/item/itemEdit");
	}
	
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public void update(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id,
			String name,
			String pathKey,
			String path,
			String title,
			String keywords,
			String description) throws Exception {
		
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		Item item = new Item();
		item.setId(id);
		item.setName(name);
		item.setState(1);
		item.setPath(path);
		item.setPathKey(pathKey);
		item.setTitle(title);
		item.setKeywords(keywords);
		item.setDescription(description);
		itemDao.update(item);
		
		response.sendRedirect("/admin/item/index");
	}
	
	@RequestMapping(value="/insert", method = RequestMethod.POST)
	public void insert(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			String name,
			String pathKey,
			String path,
			String title,
			String keywords,
			String description) throws Exception {
		
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		Item item = new Item();
		item.setName(name);
		item.setState(1);
		item.setPath(path);
		item.setPathKey(pathKey);
		item.setTitle(title);
		item.setKeywords(keywords);
		item.setDescription(description);
		itemDao.insert(item);
		
		response.sendRedirect("/admin/item/index");
	}
	
	/**
	 * 冻结
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:23
	 * @version V1.0
	 */

	@RequestMapping(value="/freeze", method = RequestMethod.GET)
	public void freeze(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		Item item = itemDao.findByPrimaryKey(id);
		item.setState(0);
		itemDao.update(item);
		
		response.sendRedirect("/admin/item/index");
	}
	
	/**
	 * 恢复正常状态
	 *	
	 * @param request
	 * @param response
	 * @param modelMap
	 * @param id
	 * @throws Exception
	 * @author wuzongbao
	 * @date 2013-3-3 下午11:48:43
	 * @version V1.0
	 */
	@RequestMapping(value="/restore", method = RequestMethod.GET)
	public void restore(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,
			int id) throws Exception {
		
		ItemDaoImpl itemDao = ItemDaoImpl.getInstance();
		Item item = itemDao.findByPrimaryKey(id);
		item.setState(1);
		itemDao.update(item);
		
		response.sendRedirect("/admin/item/index");
	}
}
